<template>
    <ComposeRule v-model:rule="addRule" @save:rule="addRuleCallback" save-rule-btn-text="添加规则" />
</template>

<script lang="ts" setup>
    import ComposeRule from "@/components/ComposeRule.vue";
    import { composeRuleApi } from "@/api/esArticle";
    import { message } from "ant-design-vue";
    import { randomId } from "@/utils/common";
    defineOptions({
        name: "AddComposeRule"
    });
    const addRule: PostComposeRuleParmas = reactive({
        rule_name: "",
        description: "",
        generate_host: "http://192.168.1.92:9200",
        generate_index: "",
        create_id: 0,
        website_group_id: 9,
        trade_type_id: 1,
        keywords: "",
        is_target: 0,
        content_type: "",
        title_data: {
            index: "",
            host: "http://192.168.1.92:9200",
            content_field: "",
            isJoin: false,
            prefix: "",
            suffix: "",
            joinChar: "",
            joinPosition: ""
        },
        img_data: {
            index: "",
            host: "http://192.168.1.92:9200",
            content_field: ""
        },
        random_link_data: {
            links: "",
            position: "top"
        },
        first_section_data: {
            index: "",
            host: "http://192.168.1.92:9200",
            content_field: "",
            field_select: { value: "", field: "" }
        },
        footer_section_data: {
            index: "",
            host: "http://192.168.1.92:9200",
            content_field: "",
            title_field: ""
        },
        main_body_section_data: [
            {
                id: randomId(),
                value: {
                    index: "",
                    host: "http://192.168.1.92:9200",
                    content_field: "",
                    title_field: "",
                    field_select: { value: "", field: "" }
                }
            }
        ],
        reply_data: {
            index: "",
            host: "http://192.168.1.92:9200",
            content_field: "",
            title_field: ""
        },
        undergo_data: {
            index: "",
            host: "http://192.168.1.92:9200",
            title_field: "",
            content_field: ""
        },
        table_data: {
            index: "",
            host: "http://192.168.1.92:9200",
            content_field: ""
        },
        sentence_data: {
            index: "",
            host: "http://192.168.1.92:9200",
            title_field: "",
            content_field: ""
        },
        content_data: [],
        replace_data: {
            sensitive_words: {
                is_index: false,
                keywords: "",
                index: "",
                host: "http://192.168.1.92:9200",
                content_field: ""
            },
            impurity: {
                is_index: false,
                keywords: "",
                index: "",
                host: "http://192.168.1.92:9200",
                content_field: ""
            }
        }
    });
    const addRuleCallback = () => {
        composeRuleApi(addRule, "post")
            .then((res) => {
                if (res.data.code === 200) {
                    message.success("操作成功!");
                } else if (res.data.code === 400) {
                    message.error(res.data.msg);
                } else {
                    message.error("服务器错误!");
                }
            })
            .catch(() => {
                message.error("服务器错误!");
            });
    };
</script>

<style scoped lang="scss"></style>
